package utils;


import base.AuthConst;
import com.wondertek.esmp.esms.empp.EMPPConnectResp;
import com.wondertek.esmp.esms.empp.EmppApi;
import listener.RecvListener;

/**
 * 功能描述:发送短信验证码
 *
 * @ClassName: SMSUtils
 * @Author: xiaoqianh丶
 * @Date: 2019-03-15 17:07
 * @Version: V1.0
 */
public class SMSUtils {

    public static boolean sendMessage(String phone, String message) {
        EmppApi emppApi = new EmppApi();
        RecvListener listener = new RecvListener(emppApi);
        EMPPConnectResp response;
        try {
            //建立同服务器的连接
            response = emppApi.connect(AuthConst.MESSAGE.HOST, AuthConst.MESSAGE.PORT, AuthConst.MESSAGE.ACCOUNTID,
                    AuthConst.MESSAGE.PASSWORD, listener);
            System.out.println(response);
            if (response == null) {
                System.out.println("连接超时失败");
                return false;
            }
            if (!emppApi.isConnected()) {
                System.out.println("连接失败:响应包状态位=" + response.getStatus());
                return false;
            }
        } catch (Exception e) {
            System.out.println("发生异常，导致连接失败");
            e.printStackTrace();
            return false;
        }

        //  发送短信
        if (emppApi.isSubmitable()) {

            //简单方式发送短信,支持长短信
            try {
                emppApi.submitMsgAsync(message, new String[]{phone}, AuthConst.MESSAGE.SERVICEID);

                //同步发送方式update 20060307
                //EMPPSubmitSMResp []  resp = emppApi.submitMsg(content,mobiles,serviceId);
                //System.out.println("resp result:"+resp[0].getResult());

            } catch (Exception e1) {
                e1.printStackTrace();
                return false;
            } finally {
                emppApi.setConnected(false);
            }
        }
        return true;
    }

}


